import { Module } from 'vuex'

interface Item {
  label: string,
  isSuccess: boolean
}

interface State {
  list: Array<Item>
}

let data = [
  {label: '吃饭', isSuccess: true},
  {label: '睡觉', isSuccess: false},
]
const todolist: Module<State, any> = {
  namespaced: true,
  state: {
    list: data
  },
  mutations: {
    ADDTASK (state, value) {
      state.list.unshift({label: value, isSuccess: false})
    },
    CHECKTASK (state, data) {
      const { index, value } = data
      state.list[index].isSuccess = value
    },
    SEARCH (state, value) {
      if (!value || value === '') {
        state.list = data
      } else {
        state.list = data.filter(item => item.label.includes(value))
      }
    },
    TYPECHANGE (state, value) {
      console.log(value)
      if (value === 0) {
        state.list = data
        return
      }
      const isSuccess = value === 1
      console.log(isSuccess)
      state.list = data.filter(item => item.isSuccess === isSuccess)
      console.log(state.list)
    },
    EDIT (state, data) {
      const { index, value } = data
      state.list[index].label = value
    },
    DEL (state, index) {
      state.list.splice(index, 1)
      console.log(state.list)
    }
  },
  actions: {
    add ({ commit }, value) {

    }
  },
  getters: {
  }
}

export default todolist